package com.zhentao.controller;

import com.zhentao.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传控制器
 * 
 * @author zhentao
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private MinioService minioService;

    /**
     * 通用文件上传
     * 
     * @param file 文件
     * @param folder 文件夹路径（可选）
     * @return 上传结果
     */
    @PostMapping("/upload")
    public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file,
                                        @RequestParam(value = "folder", required = false) String folder) {
        Map<String, Object> result = new HashMap<>();

        try {
            if (file.isEmpty()) {
                result.put("success", false);
                result.put("message", "文件不能为空");
                return result;
            }

            String fileUrl = minioService.uploadFile(file, folder);
            if (fileUrl != null) {
                result.put("success", true);
                result.put("message", "文件上传成功");
                result.put("fileUrl", fileUrl);
                result.put("fileName", file.getOriginalFilename());
                result.put("fileSize", file.getSize());
            } else {
                result.put("success", false);
                result.put("message", "文件上传失败");
            }

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "文件上传异常: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 上传药品图片
     */
    @PostMapping("/upload/medicine")
    public Map<String, Object> uploadMedicineImage(@RequestParam("file") MultipartFile file) {
        return uploadFile(file, "medicine");
    }

    /**
     * 上传用户头像
     */
    @PostMapping("/upload/avatar")
    public Map<String, Object> uploadAvatar(@RequestParam("file") MultipartFile file) {
        return uploadFile(file, "avatar");
    }

    /**
     * 上传文章图片
     */
    @PostMapping("/upload/article")
    public Map<String, Object> uploadArticleImage(@RequestParam("file") MultipartFile file) {
        return uploadFile(file, "article");
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/delete")
    public Map<String, Object> deleteFile(@RequestParam("objectName") String objectName) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean deleted = minioService.deleteFile(objectName);
            result.put("success", deleted);
            result.put("message", deleted ? "文件删除成功" : "文件删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "文件删除异常: " + e.getMessage());
        }
        return result;
    }

    /**
     * 检查文件是否存在
     */
    @GetMapping("/exists")
    public Map<String, Object> fileExists(@RequestParam("objectName") String objectName) {
        Map<String, Object> result = new HashMap<>();
        try {
            minioService.getFileInfo(objectName);
            result.put("success", true);
            result.put("exists", true);
            result.put("message", "文件存在");
        } catch (Exception e) {
            result.put("success", false);
            result.put("exists", false);
            result.put("message", "文件不存在或检查失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 获取文件信息
     */
    @GetMapping("/info")
    public Map<String, Object> getFileInfo(@RequestParam("objectName") String objectName) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("success", true);
            result.put("info", minioService.getFileInfo(objectName));
            result.put("message", "获取文件信息成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("info", null);
            result.put("message", "获取文件信息异常: " + e.getMessage());
        }
        return result;
    }
}

